//
//  PEPlantTopoItemCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/1/31.
//

#import "RHHomePlantTopoItemCell.h"
#import "RHHomeTopoHEBATView.h"
#import "RHHomeTopoMEView.h"

NSString *const PEHomeTopoHEBatCell = @"PEHomeTopoHEBatCell";
NSString *const PEHomeTopoHEAioCell = @"PEHomeTopoHEAioCell";
NSString *const PEHomeTopoMECell    = @"PEHomeTopoMECell";

@interface RHHomePlantTopoItemCell()
@property (nonatomic, strong) RHHomeTopoHEBATView *heTopoView;

@property (nonatomic, strong) RHHomeTopoMEView *meTopoView;

@end
@implementation RHHomePlantTopoItemCell
-(instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]){
        [self setupUI];
    }
    return self;
}

-(void)setProductType:(ProductType)productType
{
    _productType = productType;
    if(productType == ProductType_HE_BAT||
       productType == ProductType_HE_ALLINONE){
        self.heTopoView.hidden = NO;
        self.meTopoView.hidden = YES;
    }else if (productType == ProductType_ME){
        self.heTopoView.hidden = YES;
        self.meTopoView.hidden = NO;
    }
}

-(void)setPlantModel:(RHPlantModel *)plantModel
{
    _plantModel = plantModel;
    self.plnatNameLab.text = plantModel.plantName;
    [self getStationDetail];
}

-(void)setupUI
{
    kWeakSelf;
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.clearColor;
    
    UIView *bgView = kView;
    bgView.backgroundColor = UIColor.whiteColor;
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 15, 0, 15));

    }];
   
    
    QMUILabel *titleLab = [QMUILabel labelWithText:@"能量流向".rh_localized font:kBOLDSYSTEMFONT(16) textColor:kCOLOR_HEX(@"#171A1D")];
    [titleLab sizeToFit];
    [bgView addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.mas_offset(15);
        make.height.offset(titleLab.height+1);
    }];
    
    _plnatNameLab = [[QMUILabel alloc]init];
    _plnatNameLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:0.6];
    _plnatNameLab.font = kSYSTEMFONT(14);
    [bgView addSubview:_plnatNameLab];
    [_plnatNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleLab);
        make.right.mas_offset(-15);
    }];
    
    UIView *line = kView;
    line.backgroundColor = kCOLOR_HEX_A(@"#7E868E", 0.16);
    [bgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(titleLab.mas_bottom).offset(15);
        make.height.offset(1);
    }];
    
    _heTopoView = [[RHHomeTopoHEBATView alloc]initWithOrientation:NO];
    _heTopoView.backgroundColor = [UIColor whiteColor];
    [bgView addSubview:_heTopoView];
    [_heTopoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(line.mas_bottom);
        make.bottom.mas_offset(-15);
    }];
    
    _meTopoView = [[RHHomeTopoMEView alloc]init];
    _meTopoView.backgroundColor = [UIColor whiteColor];
    _meTopoView.hidden = YES;
    [bgView addSubview:_meTopoView];
    [_meTopoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(line.mas_bottom);
        make.bottom.mas_offset(-15);
    }];
    
    QMUIButton *fullBtn = [[QMUIButton alloc]init];
    [fullBtn setImage:[UIImage imageNamed:@"kwt_full_screen"] forState:UIControlStateNormal];
    [bgView addSubview:fullBtn];
    [fullBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(35);
        make.top.equalTo(line.mas_bottom).offset(8);
        make.right.mas_offset(-8);
    }];
    [[fullBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if(!weakSelf.plantModel){
            [QMUITips showInfo:@"没有电站".rh_localized];
            return;
        }
        if(weakSelf.fullScreenAction){
            weakSelf.fullScreenAction(weakSelf.plantModel);
        }
    }];
}


//电站 光伏,馈网,电池,负载等数据
-(void)getStationDetail
{
    [RHTopoModel startGetWithCompletionAPIKey:H_getStationDetail argument:@{@"plantId":@(self.plantModel.plant_id)} requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        if(resultModel.code == 200){
            
            RHTopoModel *topoModel = resultModel.data;
            if (self.productType == ProductType_HE_BAT||self.productType == ProductType_HE_ALLINONE) {
                //self.heTopoView.topoModel = topoModel;
                [self.heTopoView setTopoModel:topoModel deviceTypeCode:self.plantModel.deviceTypeCode];
            }
            if (self.plantModel.productTypeId == ProductType_ME) {
                //233k  
                if ([self.plantModel.plantType is233DeviceType]) {
                    self.meTopoView.topoModel = topoModel;
                }
                
            }
            
        }
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        
    }];
}
@end
